<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$errormsgs=array(
		1=>'您的免费发布职位次数已用完！',
		2=>'您的vip时间已到期！',
		3=>'您的年度发布职位次数已用完！',
		4=>'您的今日发布职位次数已用完！',
	);
	if($_RQ['errormsg']){
		$errormsg=$_RQ['errormsg'];
		$return['errormsg']=$errormsgs[$errormsg];
	}
	set_html('title','购买套餐');
	$basic=$return['basic'];
	$return['redfeeset']=pdo_getall('jy_51job_merch_taocan',array('status'=>1,'uniacid'=>$_SESSION['uniacid']));
	$return['item']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','credit1','credit2','credit3','mobile','groupid','avatar','nickname'));
	$return['item']['money']=$redfeeset[0]['fee'];
	$return['vipbg']=$basic['vipbg'];
	$return['buyxieyi']=$this->plugin_setting('buyxieyi');
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		$taocan=pdo_get('jy_51job_merch_taocan',array('id'=>$postdata['taocanid'],'uniacid'=>$_SESSION['uniacid']));;
		$fee=$taocan['money'];
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$merchid,
			'taocanid'=>$postdata['taocanid'],
			'ordersn'=>get_ordersn('jy_51job_viplog','ordersn'),
			'fee'=>$fee,
			'packname'=>$taocan['title'],
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_51job_viplog',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买套餐';
		$params['id']=$id;
		$params['plugin']='jy_51job';
		//$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		//params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_51job/merch.buyvip/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);	
		pdo_update('jy_51job_viplog',array('pay_id'=>$return['payid']),array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_51job_viplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_51job_viplog',array('status'=>1,'ispay'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	//计算到期时间
	$startTime =time();
	$taocan=pdo_get('jy_51job_merch_taocan',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['taocanid']));
	$user=pdo_get('jy_51job_merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']));
	if($user['endtime']!=0){
		$addTime=$user['endtime']+$taocan['days']*24*3600;
	}else{
		$addTime =$startTime+$taocan['days']*24*3600;
	}
	$update=array(
		'jobnum'=>$taocan['jobnum'],
		'jobnum_day'=>$taocan['jobnum_day'],
		'notenum'=>$taocan['notenum'],
		'daynum'=>$taocan['notenum_day'],
		'taocanid'=>$taocan['id'],
		'isvip'=>1,
		'endtime'=>$addTime
	);
	pdo_update('jy_51job_merch',$update,array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']));
	exi('支付成功','success','jy_51job/merch.index/');
}